package com.qf.shop.system.modules.user.controller;

import java.util.Arrays;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.shop.common.core.utils.R;
import com.qf.shop.system.utils.PageUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.qf.shop.system.modules.user.entity.WeiXinUserEntity;
import com.qf.shop.system.modules.user.service.WeiXinUserService;


/**
 * @author Java21110
 * @email java2110@gmail.com
 * @date 2022-04-15 17:18:28
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private WeiXinUserService weiXinUserService;

    @GetMapping("/getUserInfo/{userId}")
    public WeiXinUserEntity getUserInfo(@PathVariable Integer userId) {

        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("id", userId);
        WeiXinUserEntity userEntity = weiXinUserService.getOne(queryWrapper);
        userEntity.setPassword(null); // 用户的密码不能返回给前端
        return userEntity;
    }

    @RequestMapping("/register")
    public WeiXinUserEntity register(@RequestBody WeiXinUserEntity user) {
        boolean save = weiXinUserService.save(user);
        return user;
    }

    @GetMapping("/findUserByPhone/{phone}")
    public WeiXinUserEntity findUserByPhone(@PathVariable("phone") String phone) {
        QueryWrapper<WeiXinUserEntity> wrapper = new QueryWrapper();
        wrapper.eq("phone", phone);
        return weiXinUserService.getOne(wrapper);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("user:user:list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = weiXinUserService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("user:user:info")
    public R info(@PathVariable("id") Integer id) {
        WeiXinUserEntity user = weiXinUserService.getById(id);

        return R.ok().put("user", user);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("user:user:save")
    public R save(@RequestBody WeiXinUserEntity user) {
        weiXinUserService.save(user);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("user:user:update")
    public R update(@RequestBody WeiXinUserEntity user) {
        weiXinUserService.updateById(user);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("user:user:delete")
    public R delete(@RequestBody Integer[] ids) {
        weiXinUserService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
